package com.ruoyi.mes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.mes.domain.*;
import com.ruoyi.mes.service.IDeviceInfoService;
import com.ruoyi.mes.vo.product.BomMtrVo;
import com.ruoyi.mes.vo.product.ProTecVo;
import com.ruoyi.mes.vo.product.TecProceVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.service.IProInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品信息Controller
 *
 * @author LongAndHao
 * @date 2023-04-11
 */
@RestController
@RequestMapping("/mes/proinfo")
public class ProInfoController extends BaseController {
    @Autowired
    private IProInfoService proInfoService;

    @Autowired
    private IDeviceInfoService deviceInfoService;

    /**
     * 查询产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('mes:proinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProInfo proInfo) {
        startPage();
        List<ProInfo> list = proInfoService.selectProInfoList(proInfo);
        return getDataTable(list);
    }

    /**
     * 导出产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('mes:proinfo:export')")
    @Log(title = "产品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProInfo proInfo) {
        List<ProInfo> list = proInfoService.selectProInfoList(proInfo);
        ExcelUtil<ProInfo> util = new ExcelUtil<ProInfo>(ProInfo.class);
        util.exportExcel(response, list, "产品信息数据");
    }

    /**
     * 获取产品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:proinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(proInfoService.selectProInfoById(id));
    }

    /**
     * 新增产品信息
     */
    @PreAuthorize("@ss.hasPermi('mes:proinfo:add')")
    @Log(title = "产品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProInfo proInfo) {
//        if (proInfoService.insertProInfo(proInfo) < 0) {
//            return AjaxResult.error("产品编码重复!");
//        }
        int i = proInfoService.insertProInfo(proInfo);
        if (i > 0) {
            return success();
        }else {
            return AjaxResult.error("产品编码重复");
        }

    }

    /**
     * 修改产品信息
     */
    @PreAuthorize("@ss.hasPermi('mes:proinfo:edit')")
    @Log(title = "产品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProInfo proInfo) {
        return toAjax(proInfoService.updateProInfo(proInfo));
    }

    /**
     * 删除产品信息
     */
    @PreAuthorize("@ss.hasPermi('mes:proinfo:remove')")
    @Log(title = "产品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(proInfoService.deleteProInfoByIds(ids));
    }

    //***************************************************************************************

    /**
     * 查-Bom及原料-产品Id
     *
     * @param id 产品id
     * @return
     */
    @GetMapping("/getBomAndMtrByProId")
    public R<BomMtrVo> getBomAndMtrByProId(@RequestParam("id") Long id) {
        return proInfoService.getBomAndMtrByProId(id);
    }


    /**
     * 查 - 工艺工序等 - 产品id
     *
     * @param id 产品id。
     * @return
     */
    @GetMapping("/getTecProceByProId")
    public R<List<TecProceVo>> getTecProceByProId(@RequestParam("id") Long id) {
        return proInfoService.getTecProceByProId(id);
    }

    /**
     * 根据工艺编码、工序Id获取原料信息
     *
     * @param tecCode 工艺编码
     * @param proceId 工序id
     * @return
     */
    @GetMapping("/getMtrByProceId")
    public List<ProceMtr> getMtrByProceId(@RequestParam String tecCode, @RequestParam Long proceId) {
        return proInfoService.getMtrByProceId(tecCode, proceId);
    }

    /**
     * 根据工艺编码、工序id查询设备信息
     *
     * @param tecCode
     * @param proceId
     * @return
     */
    @GetMapping("/getDevicesByProceIdAndTecCode")
    public List<DeviceInfo> getDevicesByProceIdAndTecCode(@RequestParam String tecCode, @RequestParam Long proceId) {
        return deviceInfoService.getDevicesByProceIdAndTecCode(tecCode, proceId);
    }

    /**
     * 编辑-工艺、工序、原料、设备
     *
     * @param proTecVo
     * @return
     */
    @PostMapping("/editProTecAndMtrs")
    public AjaxResult editProTecAndMtrs(@RequestBody ProTecVo proTecVo) {
        return proInfoService.editProTecAndMtrs(proTecVo);
    }

    /**
     * 编辑-产品对应的Bom、原料等信息
     * @param bomMtrVo
     * @return
     */
    @PostMapping("/editProBomAndMtrs")
    public AjaxResult editProBomAndMtrs(@RequestBody BomMtrVo bomMtrVo) {
        return proInfoService.editProBomAndMtrs(bomMtrVo);
    }

    /**
     * 查-工艺-产品id
     *
     * @param proId
     * @return
     */
    @GetMapping("/getProTec")
    public R<ProTecVo> getProTec(@RequestParam Long proId) {
        return proInfoService.getProTec(proId);
    }

    /**
     * 删-产品-产品id
     * @param proId
     * @return
     */
    @DeleteMapping("/deleteProInfo")
    public AjaxResult deleteProInfo(@RequestParam Long proId)
    {
        return proInfoService.deleteProInfo(proId);
    }

}
